package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.vo.PCountVo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fenge
 */
@Service
public class PurchaseListGoodsServiceImpl implements PurchaseListGoodsService {

    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;

    @Autowired
    private PurchaseListDao purchaseListDao;

    @Override
    public void save(PurchaseList purchaseList, String purchaseListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        Integer userId = user.getUserId();
        purchaseList.setUserId(userId);
        purchaseListDao.save(purchaseList);
        Gson gson = new Gson();
        List<PurchaseListGoods> purchaseListGoods = gson.fromJson(purchaseListGoodsStr,new TypeToken<List<PurchaseListGoods>>(){}.getType());
        purchaseListGoods.forEach(purchaseListGood->{
            purchaseListGood.setPurchaseListId(purchaseList.getPurchaseListId());
            purchaseListGoodsDao.save(purchaseListGood);
        });
    }

    @Override
    public Map<String, Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        Map<String, Object> map =new HashMap<>();
        List<PurchaseList> purchaseListGoods = purchaseListDao.list(purchaseNumber,supplierId,state,sTime,eTime);
        map.put("rows",purchaseListGoods);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer purchaseListId) {
        Map<String, Object> map =new HashMap<>();
        List<PurchaseListGoods> purchaseListGoods = purchaseListGoodsDao.goodsList(purchaseListId);
        map.put("rows",purchaseListGoods);
        return map;
    }

    @Override
    public void delete(Integer purchaseListId) {
        List<PurchaseListGoods> purchaseListGoods = purchaseListGoodsDao.goodsList(purchaseListId);
        purchaseListGoods.forEach(purchaseListGood->{
            purchaseListGoodsDao.delete(purchaseListGood);
        });
        purchaseListDao.delete(purchaseListId);
    }

    @Override
    public void updateState(Integer purchaseListId) {
        purchaseListDao.updateState(purchaseListId);
    }

    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<PCountVo> pCountVos = purchaseListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String s = gson.toJson(pCountVos);
        return s;

        //SELECT t1.purchase_number number,purchase_date date,t2.supplier_name supplierName,t3.goods_code `code`,t3.goods_name `name`,t3.goods_model model,t4.goods_type_name goodsType,t3.goods_unit unit,t3.price price,t3.goods_num num,t3.total total
        //FROM t_purchase_list t1
        //LEFT JOIN t_supplier t2
        //ON t1.supplier_id = t2.supplier_id
        //LEFT JOIN t_purchase_list_goods t3
        //ON t1.purchase_list_id = t3.purchase_list_id
        //LEFT JOIN t_goods_type t4
        //ON t3.goods_type_id = t4.goods_type_id
    }
}
/*
[{"goodsId":44,
"goodsTypeId":11,
"goodsCode":"0031",
"goodsName":"234",
"goodsModel":"234",
"goodsUnit":"元",
"lastPurchasingPrice":0,
"price":"100",
"goodsNum":"100",
"total":10000},{"goodsId":43,"goodsTypeId":9,"goodsCode":"9d9c6970-bbcb-468a-bc43-2aaa16e0f874","goodsName":"456","goodsModel":"456","goodsUnit":"瓶","lastPurchasingPrice":0,"price":"100","goodsNum":"100","total":10000}]
 */
